package servlet;

import dao.PlaceDAO;
import pojo.Place;
import utils.ApiResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/place")
public class PlaceServlet extends BaseServlet {

    // 获取所有景点信息
    public void getAllPlaces(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            List<Place> places = PlaceDAO.getAllPlaces();
            sendJsonResponse(resp, new ApiResult(200, "获取所有景点信息成功", places));
        } catch (SQLException e) {
            sendJsonResponse(resp, new ApiResult(500, "获取所有景点信息失败", e.getMessage()));
        }
    }

    // 根据ID获取景点信息
    public void getPlaceById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int placeId = Integer.parseInt(req.getParameter("placeId"));
        try {
            Place place = PlaceDAO.getPlaceById(placeId);
            if (place != null) {
                sendJsonResponse(resp, new ApiResult(200, "获取景点信息成功", place));
            } else {
                sendJsonResponse(resp, new ApiResult(404, "景点信息不存在", null)); // 404 Not Found
            }
        } catch (SQLException e) {
            sendJsonResponse(resp, new ApiResult(500, "获取景点信息失败", e.getMessage()));
        }
    }

    // 新增景点信息
    public void insertPlace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String description = req.getParameter("description");
        String location = req.getParameter("location");
        String tickets = req.getParameter("tickets");
        String openHours = req.getParameter("openHours");
        String imageUrl = req.getParameter("imageUrl");

        Place place = new Place();
        place.setName(name);
        place.setDescription(description);
        place.setLocation(location);
        place.setTickets(tickets);
        place.setOpenHours(openHours);
        place.setImageUrl(imageUrl);

        try {
            PlaceDAO.insertPlace(place);
            sendJsonResponse(resp, new ApiResult(200, "新增景点信息成功", null));
        } catch (SQLException e) {
            sendJsonResponse(resp, new ApiResult(500, "新增景点信息失败", e.getMessage()));
        }
    }

    // 更新景点信息
    public void updatePlace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int placeId = Integer.parseInt(req.getParameter("placeId"));
        String name = req.getParameter("name");
        String description = req.getParameter("description");
        String location = req.getParameter("location");
        String tickets = req.getParameter("tickets");
        String openHours = req.getParameter("openHours");
        String imageUrl = req.getParameter("imageUrl");

        Place place = new Place();
        place.setPlaceId(placeId);
        place.setName(name);
        place.setDescription(description);
        place.setLocation(location);
        place.setTickets(tickets);
        place.setOpenHours(openHours);
        place.setImageUrl(imageUrl);

        try {
            PlaceDAO.updatePlace(place);
            sendJsonResponse(resp, new ApiResult(200, "更新景点信息成功", null));
        } catch (SQLException e) {
            sendJsonResponse(resp, new ApiResult(500, "更新景点信息失败", e.getMessage()));
        }
    }

    // 删除景点信息
    public void deletePlace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int placeId = Integer.parseInt(req.getParameter("placeId"));
        try {
            PlaceDAO.deletePlace(placeId);
            sendJsonResponse(resp, new ApiResult(200, "删除景点信息成功", null));
        } catch (SQLException e) {
            sendJsonResponse(resp, new ApiResult(500, "删除景点信息失败", e.getMessage()));
        }
    }
}
